#include "clock_manager.h"
#include <Arduino.h>
#include "analog_clock.h"

bool maybeUpdateClock(Panel &panel, NTPTime &ntpTime, unsigned long &lastClockUpdate, unsigned long clockIntervalMs, int rightTzOffsetHours) {
    if (millis() - lastClockUpdate <= clockIntervalMs) return false;
    lastClockUpdate = millis();

    uint8_t hourL, minuteL, secondL, wdayL;
    const char* weekDayStrL = "";
    static const char* WEEKDAYS[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

    int yearL=0, monthL=0, dayL=0;
    if (ntpTime.isTimeValid()) {
        // left panel: use configured local time getters
        hourL = ntpTime.getHour();
        minuteL = ntpTime.getMinute();
        secondL = ntpTime.getSecond();
        time_t now = time(nullptr);
        struct tm timeinfo;
        localtime_r(&now, &timeinfo);
        wdayL = timeinfo.tm_wday; // 0=Sunday
        yearL = timeinfo.tm_year + 1900;
        monthL = timeinfo.tm_mon + 1;
        dayL = timeinfo.tm_mday;
    } else {
        unsigned long totalSeconds = millis() / 1000;
        secondL = totalSeconds % 60;
        minuteL = (totalSeconds / 60) % 60;
        hourL = (totalSeconds / 3600) % 24;
        wdayL = (totalSeconds / 86400 + 4) % 7; // fallback weekday
        // crude fallback date (days since epoch)
        unsigned long days = totalSeconds / 86400;
        yearL = 1970;
        monthL = 1;
        dayL = (days % 30) + 1;
    }
    weekDayStrL = WEEKDAYS[wdayL % 7];

    // Draw the clock in the left square area (local time)
    drawAnalogClock(panel, 0, 0, panel.height, panel.height, hourL, minuteL, secondL, weekDayStrL, yearL, monthL, dayL, "SH");

    // Right panel: get time for requested timezone offset (hours)
    uint8_t hourR=0, minuteR=0, secondR=0, wdayR=0;
    int yearR=0, monthR=0, dayR=0;
    const char* weekDayStrR = "";
    bool haveR = false;
    if (ntpTime.isTimeValid()) {
        if (rightTzOffsetHours == -8) {
            // Use DST-aware Pacific Time helper
            haveR = ntpTime.getPacificTime(hourR, minuteR, secondR, wdayR, yearR, monthR, dayR);
        } else {
            haveR = ntpTime.getTimeForOffset(rightTzOffsetHours, hourR, minuteR, secondR, wdayR, yearR, monthR, dayR);
        }
    } else {
        // if no NTP, approximate by adjusting system-millis derived time
        unsigned long total = millis() / 1000;
        time_t base = total; // seconds since start
        base += rightTzOffsetHours * 3600; // crude
        struct tm t;
        gmtime_r(&base, &t);
        hourR = t.tm_hour; minuteR = t.tm_min; secondR = t.tm_sec; wdayR = t.tm_wday;
        yearR = t.tm_year + 1900; monthR = t.tm_mon + 1; dayR = t.tm_mday;
        haveR = true;
    }
    if (haveR) {
        weekDayStrR = WEEKDAYS[wdayR % 7];
        int rightX = panel.width - panel.height;
        if (rightX > 0) {
            const char* rightLocation = (rightTzOffsetHours == -8) ? "SF" : "TZ";
            drawAnalogClock(panel, rightX, 0, panel.height, panel.height, hourR, minuteR, secondR, weekDayStrR, yearR, monthR, dayR, rightLocation);
        }
    }

    // Serial debug output: left (local) and right (Pacific or offset) times for verification
    Serial.printf("Left(local): %04d-%02d-%02d %02d:%02d:%02d %s\n", yearL, monthL, dayL, hourL, minuteL, secondL, weekDayStrL);
    if (haveR) {
        if (rightTzOffsetHours == -8) {
            Serial.printf("Right(Pacific): %04d-%02d-%02d %02d:%02d:%02d %s\n", yearR, monthR, dayR, hourR, minuteR, secondR, weekDayStrR);
        } else {
            Serial.printf("Right(offset %d): %04d-%02d-%02d %02d:%02d:%02d %s\n", rightTzOffsetHours, yearR, monthR, dayR, hourR, minuteR, secondR, weekDayStrR);
        }
    } else {
        Serial.println("Right: time unavailable");
    }

    return true;
}
